package com.dongdongshop.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.QuerySendDetailsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.dongdongshop.service.PhoneService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import com.aliyun.teaopenapi.models.*;


@Service
public class PhoneServiceImpl implements PhoneService {

    @Override
    public void sendPhoneMessage(String phoneNum) {
        //java.util.List<String> args = java.util.Arrays.asList(args_);
        Client client = null;
        try {
            client = createClient("LTAI4GCbdwnjCGwQj9Z3tr3N", "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
        } catch (Exception e) {
            e.printStackTrace();
        }

        String code = RandomStringUtils.randomNumeric(6);

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNum)//手机号
                .setSignName("东科创想")//签名
                .setTemplateCode("SMS_162522027")//模板名称
                .setTemplateParam("{\"sms_code\":\"" + code + "\"}");//模板需要的变量

        // 复制代码运行请自行打印 API 的返回值
        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            SendSmsResponseBody body = sendSmsResponse.getBody();
            System.out.println("阿里云的返回码:"+body.getCode());
            System.out.println("阿里云返回信息:"+body.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

}
